/**
 * @ClassName:     MainActivity
 * @Description:   MainActivity
 *
 * @author          JetKey
 * @version         V1.0
 * @Date            2018/08/10
 */

package com.imi.sdk.jnitest

import android.app.Activity
import android.app.Fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import org.jetbrains.anko.*
import kotlin.concurrent.thread

class MainActivity : Activity(){

    init {
        System.loadLibrary("testsuit_jni")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val v = UI {


            verticalLayout {
                textView(""){
                    txtOut = this
//                    lparams(matchParent,dip(200))
                }
                verticalLayout {
                    id = android.R.id.home
                }
            }
        }.view
        setContentView(v)
        //
        fragmentManager.beginTransaction().apply {
            add(android.R.id.home, ListFragment(),"home")
            commit()
        }
    }

    var txtOut:TextView? = null

    //
    fun print(text:String):Int{
        runOnUiThread {
            txtOut?.append("${text}\n")
        }
        return 1
    }

    fun getTestList() = Array<String>(getTestCount()) { getTestName(it) }


//    external fun getUrl():String
    external fun getTestCount():Int
    external fun getTestName(i:Int):String
    external fun runTest(i:Int):Int

    external fun setHandles(cam:Long,dev:Long,stream:Long):Int



    class ListFragment: Fragment(){
        val master by lazy {
            activity as? MainActivity
        }
        override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View = UI{
            verticalLayout {
                textView("Test Case Count:${master?.getTestCount()}")

                scrollView {
                    verticalLayout {

                        master!!.getTestList()!!.forEachIndexed { index, s ->
                            //
                            button(s) {
                                setOnClickListener {
                                    activity.fragmentManager.beginTransaction().apply {
                                        val f = CameraPreviewFragment()
                                        f.testId = index
                                        replace(android.R.id.home,f,"CameraPreview")
                                        addToBackStack("home")
                                        commit()
                                    }
                                }
                            }
                        }
                    }

                }
            }
        }.view
    }

}